package com.youlai.boot.modules.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.game.mapper.GameCategoryMapper;
import com.youlai.boot.modules.game.model.entity.GameCategory;
import com.youlai.boot.modules.game.model.form.GameCategoryForm;
import com.youlai.boot.modules.game.service.GameCategoryService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 游戏分类服务实现类
 *
 * @author 有来技术团队
 */
@Service
@RequiredArgsConstructor
public class GameCategoryServiceImpl extends ServiceImpl<GameCategoryMapper, GameCategory> implements GameCategoryService {

    @Override
    public Page<GameCategory> getCategoryPage(Integer pageNum, Integer pageSize, String keywords) {
        LambdaQueryWrapper<GameCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(keywords)) {
            queryWrapper.like(GameCategory::getName, keywords)
                    .or()
                    .like(GameCategory::getCode, keywords);
        }
        queryWrapper.orderByAsc(GameCategory::getSort);

        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public List<GameCategory> getAllCategories() {
        LambdaQueryWrapper<GameCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(GameCategory::getSort);
        return list(queryWrapper);
    }

    @Override
    public GameCategory getCategoryById(Long id) {
        return getById(id);
    }

    @Override
    public Long addCategory(GameCategoryForm formData) {
        GameCategory entity = new GameCategory();
        entity.setName(formData.getName());
        entity.setCode(formData.getCode());
        entity.setIcon(formData.getIcon());
        entity.setSort(formData.getSort());

        save(entity);
        return entity.getId();
    }

    @Override
    public boolean updateCategory(Long id, GameCategoryForm formData) {
        GameCategory entity = getById(id);
        if (entity == null) {
            return false;
        }

        entity.setName(formData.getName());
        entity.setCode(formData.getCode());
        entity.setIcon(formData.getIcon());
        entity.setSort(formData.getSort());

        return updateById(entity);
    }

    @Override
    public boolean deleteCategories(List<Long> ids) {
        return removeByIds(ids);
    }
} 